//
//  ItemCellView.m
//  SlashEM
//
//  Created by dirk on 1/6/10.
//  Copyright 2010 Dirk Zimmermann. All rights reserved.
//

//  This file is part of Slash'EM.
//
//  Slash'EM is free software: you can redistribute it and/or modify
//  it under the terms of the GNU General Public License as published by
//  the Free Software Foundation, version 2 of the License only.
//
//  Slash'EM is distributed in the hope that it will be useful,
//  but WITHOUT ANY WARRANTY; without even the implied warranty of
//  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
//  GNU General Public License for more details.
//
//  You should have received a copy of the GNU General Public License
//  along with Slash'EM.  If not, see <http://www.gnu.org/licenses/>.

#import "ItemCellView.h"
#import "NhItem.h"
#import "UIFactory.h"
#import "NSString+NetHack.h"
#import "NhMenuWindow.h"

@implementation ItemCellView

@synthesize item;
@synthesize menuWindow;

- (id)initWithFrame:(CGRect)frame {
    if (self = [super initWithFrame:frame]) {
		self.autoresizingMask = UIViewAutoresizingFlexibleWidth;

		titleLabel = [[UILabel alloc] initWithFrame:CGRectZero];
		titleLabel.autoresizingMask = self.autoresizingMask;
		titleLabel.font = [UIFactory displayTextFont];
		[self addSubview:titleLabel];
		[titleLabel release];
		
		descriptionLabel = [[UILabel alloc] initWithFrame:CGRectZero];
		descriptionLabel.autoresizingMask = self.autoresizingMask;
		descriptionLabel.font = [UIFactory displayTextFont];
		descriptionLabel.textColor = [UIColor grayColor];
		[self addSubview:descriptionLabel];
		[descriptionLabel release];
		
		amountSlider = [[UISlider alloc] initWithFrame:CGRectZero];
		amountSlider.autoresizingMask = self.autoresizingMask;
		[self addSubview:amountSlider];
		[amountSlider release];
	}
    return self;
}

- (NSArray *)updateTitle {
	NSArray *lines = [item.title splitNetHackDetails];
	if (menuWindow && menuWindow.how == PICK_NONE || menuWindow.how == PICK_ONE &&
		item.identifier.a_void && isalpha(item.identifier.a_char)) {
		if (item.amount == -1) {
			titleLabel.text = [NSString stringWithFormat:@"%c - %@", item.identifier.a_char, [lines objectAtIndex:0]];
		} else {
			titleLabel.text = [NSString stringWithFormat:@"%c - %d/%@", item.identifier.a_char, item.amount, [lines objectAtIndex:0]];
		}
	} else {
		if (item.amount == -1) {
			titleLabel.text = [NSString stringWithFormat:@"%@", [lines objectAtIndex:0]];
		} else {
			titleLabel.text = [NSString stringWithFormat:@"%d/%@", item.amount, [lines objectAtIndex:0]];
		}
	}
	return lines;
}

- (void)sliderUpdated:(UISlider *)sender {
	item.amount = roundf(sender.value);
	[self updateTitle];
}

- (void)setItem:(NhItem *)i {
	if (item != i) {
		[item release];
		item = [i retain];
	}

	// maximum space frame
	CGRect cellFrame = CGRectInset(self.superview.bounds, 5.0f, 0.0f);
	
	NSArray *lines = [self updateTitle];

	CGSize textSize = [titleLabel sizeThatFits:cellFrame.size];
	CGRect titleLabelFrame = cellFrame;
	titleLabelFrame.size.height = textSize.height;
	titleLabel.frame = titleLabelFrame;
	
	if (item.amount != -1) {
		descriptionLabel.frame = CGRectZero;
		descriptionLabel.hidden = YES;
		
		amountSlider.hidden = NO;
		CGRect sliderFrame = cellFrame;
		sliderFrame.size.height = 16.0f;
		sliderFrame.origin.y = 0.0f;
		amountSlider.frame = sliderFrame;
		amountSlider.minimumValue = 1.0f;
		amountSlider.maximumValue = item.maxAmount;
		amountSlider.value = item.amount;
		[amountSlider addTarget:self action:@selector(sliderUpdated:) forControlEvents:UIControlEventValueChanged];
	} else {
		amountSlider.frame = CGRectZero;
		amountSlider.hidden = YES;
		[amountSlider removeTarget:self action:@selector(sliderUpdated:) forControlEvents:UIControlEventValueChanged];
		if (lines.count == 2) {
			descriptionLabel.hidden = NO;
			descriptionLabel.text = [lines objectAtIndex:1];
			descriptionLabel.frame = titleLabel.frame;
		} else {
			descriptionLabel.frame = CGRectZero;
		}
	}

	// center all subviews vertically
	float totalHeight = 0.0f;
	for (UIView *view in self.subviews) {
		totalHeight += view.bounds.size.height;
	}
	if (totalHeight > cellFrame.size.height) {
		CGRect frame = self.frame;
		frame.size.height = totalHeight;
		self.frame = frame;
	}
	float originY = (cellFrame.size.height - totalHeight)/2;
	for (UIView *view in self.subviews) {
		CGRect frame = view.frame;
		frame.origin.y = originY;
		view.frame = frame;
		originY += frame.size.height;
	}

}

- (void)dealloc {
    [super dealloc];
}

@end
